﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace ch01
{
    public partial class FrmUpdateSutInfo : Form
    {
        public FrmUpdateSutInfo()
        {
            InitializeComponent();
        }
        public int sutNo;
        private void FrmUpdateSutInfo_Load(object sender, EventArgs e)
        {
            BindCombobox();
            string sql = string.Format("select * from student where sutno={0}",sutNo);
            SqlDataReader reader = DBHelper.SelectMore(sql);
            if (reader.Read())
            {
                this.txt_name.Text=reader["name"].ToString();
                this.txt_pwd.Text=reader["pwd"].ToString();
                this.txt_drpwd.Text=reader["pwd"].ToString();
                if (reader["sex"].ToString().Equals("男"))
                {
                    this.radioButton1.Checked =  true;
                }
                else
                {
                    this.radioButton2.Checked = true;
                }
                this.comboBox1.SelectedValue=reader["gradeid"];
                this.txt_address.Text=reader["address"].ToString();
                this.txt_phone.Text=reader["phone"].ToString();
                this.txt_Email.Text=reader["email"].ToString();
                dateTimePicker1.Value = Convert.ToDateTime(reader["bomdate"]);
            }
            reader.Close();

        }
        public void BindCombobox()
        {
            string sql = string.Format("select * from grade");
            DataTable dt = DBHelper.SelectTable(sql);
            this.comboBox1.DisplayMember = "gradename";
            this.comboBox1.ValueMember = "no";
            this.comboBox1.DataSource = dt;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string name = txt_name.Text;
            string pwd = txt_pwd.Text;
            string drpwd = txt_drpwd.Text;
            string sex = radioButton1.Checked ? "男" : "女";
            int gradeid = Convert.ToInt32(comboBox1.SelectedValue);
            string phone =txt_phone.Text;
            string address=txt_address.Text;
            string date = this.dateTimePicker1.Value.ToString("yyyy-MM-dd");
            string email = this.txt_Email.Text;

            if (string.IsNullOrEmpty(name)||string.IsNullOrEmpty(pwd)||string.IsNullOrEmpty(drpwd)
            ||string.IsNullOrEmpty(sex)||string.IsNullOrEmpty(phone)
            ||string.IsNullOrEmpty(address)||string.IsNullOrEmpty(email))
            {
                MessageBox.Show("请填写完整的信息！");
                return;
            }
            if (!pwd.Equals(drpwd))
            {
                MessageBox.Show("您输入的密码不一致！");
                return;
            }
            string sql = string.Format(@"update Student set pwd='{0}',name='{1}',sex='{2}',gradeid={3},phone='{4}',
            address='{5}',bomdate='{6}',email='{7}' where sutno={8}",pwd,name,sex,gradeid,phone,address,date,email,sutNo);
            int result = DBHelper.DataUpdate(sql);
            if (result > 0)
            {
                MessageBox.Show("修改成功！");
            }
            else
            {
                MessageBox.Show("修改失败！");
            }
        }
   
    }
}
